package de.grosjohann.android.feuerwehr.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import de.grosjohann.android.feuerwehr.R;
import de.grosjohann.android.feuerwehr.domain.Fahrzeug;
import de.grosjohann.android.feuerwehr.type.FMSStatusType;

public class FahrzeugListAdapter
  extends BaseAdapter {

  private static final String NAME = "FahrzeugListAdapter";

  private static final List<Fahrzeug> list = new ArrayList<Fahrzeug>(3);

  private final Activity context;
  private final int listViewId;
  private final boolean attachToRoot = false;

  private int selection = -1;

  public FahrzeugListAdapter(Activity ctx, int viewId) {
    context = ctx;
    listViewId = viewId;
    list.clear();
  }

  @Override
  public int getCount() {
    return list.size();
  }

  @Override
  public Fahrzeug getItem(int i) {
    return list.get(i);
  }

  @Override
  public long getItemId(int i) {
    return i;
  }

//  @Override
//  public boolean isEnabled(int position) {
//    Panel panel = new Panel(context, context);
//    panel.showInfo("test");
//    return (list.get(position).getLocation() != null) && (!list.get(position).getLocation().isEmpty());
//  }

  @Override
  public View getView(int index, View convertView, ViewGroup parent) {
    View row = null;
    Fahrzeug f = list.get(index);

    if (convertView == null) {
      row = LayoutInflater.from(context).inflate(listViewId, parent, attachToRoot);
    } else {
      row = convertView;
    }

    TextView tvBezeichnung = (TextView) row.findViewById(R.id.bezeichnung);
    tvBezeichnung.setText(f.getDescription());

    TextView tvKennung = (TextView) row.findViewById(R.id.kennung);
    tvKennung.setText(f.getIdentifier());

    FMSStatusType status = f.getStatus();
    TextView tvStatus = (TextView) row.findViewById(R.id.status);
    tvStatus.setText(String.valueOf(status.getStatus()));
    switch (status) {
      case EINSATZBEREIT_FUNK:
      case EINSATZBEREIT_GH:
        tvStatus.setTextColor(Color.GREEN);
        break;
      case EINSATZ_UEBERNOMMEN:
      case EINSATZSTELLE_AN:
      case NOTRUF:
        tvStatus.setTextColor(Color.RED);
        break;
      case NICHT_EINSATZBEREIT:
        tvStatus.setTextColor(Color.LTGRAY);
        break;
    }

    TextView tvStatusLang = (TextView) row.findViewById(R.id.statusLang);
    tvStatusLang.setText(status.getResId());

    if (index == selection) {
      //TODO: set the proper selection color here:
      row.findViewById(R.id.marker_lv).setVisibility(View.VISIBLE);
    } else {
      row.findViewById(R.id.marker_lv).setVisibility(View.INVISIBLE);
    }

    return row;
  }

  public void addAll(List<Fahrzeug> newList) {
    list.clear();
    list.addAll(newList);

    Log.d(NAME, list.toString());

    super.notifyDataSetChanged();
  }

  public void setSelection(int newSelection) {
    selection = newSelection;
  }
}
